package com.example.mydy.api;

import okhttp3.ResponseBody;
import okhttp3.RequestBody;
import com.example.mydy.BaseResponse;
import com.example.mydy.LoginResponse;
import com.example.mydy.model.AnnouncementListResponse;
import com.example.mydy.model.AdvertisementListResponse;
import com.example.mydy.model.TaskListResponse;
import com.example.mydy.model.ProjectListResponse;
import com.example.mydy.model.RewardListResponse;
import com.example.mydy.model.UserInfo;
import com.example.mydy.model.UserInfoResponse;
import com.example.mydy.model.UserListResponse;
import com.example.mydy.model.UserUpdateRequest;
import com.example.mydy.model.InvitationResponse;
import com.example.mydy.model.InviteCodeResponse;
import com.example.mydy.model.AdvertisementResponse;
import com.example.mydy.model.CommonResponse;
import com.example.mydy.model.PromotionResponse;
import com.example.mydy.model.UserData;
import com.example.mydy.model.UserTaskStatus;
import com.example.mydy.model.TaskDetailResponse;
import com.example.mydy.model.RewardTask;
import com.example.mydy.model.Project;
import com.example.mydy.model.InviterResponse;

import java.util.List;
import java.util.Map;

import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.http.*;

/**
 * API接口类，定义与服务器通信的所有方法
 */
public interface UserApi {
    @FormUrlEncoded
    @POST("/user/login")
    Call<LoginResponse> login(
        @Field("username") String username,
        @Field("password") String password
    );

    @FormUrlEncoded
    @POST("/user/register")
    Call<CommonResponse> register(
        @Field("username") String username,
        @Field("password") String password,
        @Field("nickname") String nickname
    );

    @GET("/user/info")
    Call<UserInfoResponse> getUserInfo(@Header("Authorization") String token);

    @GET("/user/data")
    Call<UserData> getUserData(@Query("username") String username);

    @GET("/user/promotions")
    Call<PromotionResponse> getPromotions();

    @GET("/user/tasks/status")
    Call<UserTaskStatus> getUserTaskStatus();

    @GET("/user/balance")
    Call<ResponseBody> getBalance(@Header("Authorization") String token);

    @POST("/user/withdraw")
    Call<CommonResponse> requestWithdrawal(@Header("Authorization") String token, @Body Map<String, Object> withdrawalData);

    @GET("/user/tasks")
    Call<TaskListResponse> getUserTasks(@Header("Authorization") String token);

    @GET("/user/rewards")
    Call<RewardListResponse> getUserRewards(@Header("Authorization") String token);

    @GET("/user/invites")
    Call<InvitationResponse> getInviteRecords(@Header("Authorization") String token);

    @POST("/user/invite/generate")
    Call<InviteCodeResponse> generateInviteCode(@Header("Authorization") String token);

    @POST("/user/invite/use")
    Call<CommonResponse> useInviteCode(@Header("Authorization") String token, @Body Map<String, String> inviteData);

    @GET("/admin/users")
    Call<CommonResponse> getAllUsers(@Header("Authorization") String token);

    @PUT("/admin/user/{userId}")
    Call<ResponseBody> updateUserById(@Path("userId") int userId, @Body RequestBody body);

    @PUT("/admin/user/{userId}/password")
    Call<ResponseBody> updateUserPassword(@Path("userId") int userId, @Body RequestBody body);

    @POST("/admin/reward")
    Call<ResponseBody> publishRewardAdmin(@Body RequestBody body);

    @POST("/admin/task")
    Call<ResponseBody> publishTaskAdmin(@Body RequestBody body);

    @POST("/admin/advertisement")
    Call<ResponseBody> publishAdvertisementAdmin(@Body RequestBody body);

    @POST("/admin/announcement")
    Call<ResponseBody> publishAnnouncementAdmin(@Body RequestBody body);

    @POST("/{path}")
    Call<ResponseBody> genericPost(@Path("path") String path, @Body RequestBody body);

    @GET("/{path}")
    Call<ResponseBody> genericGet(@Path("path") String path);

    /* 管理员接口 */
    @GET("api/admin/users")
    Call<UserListResponse> getUsers(@Header("Authorization") String authHeader);

    @PUT("api/admin/users/{userId}")
    Call<CommonResponse> updateAdmin(
            @Header("Authorization") String authHeader,
            @Path("userId") String userId,
            @Body UserUpdateRequest request
    );

    @PATCH("api/admin/users/{userId}/password")
    Call<CommonResponse> updatePasswordAdmin(
            @Header("Authorization") String authHeader,
            @Path("userId") String userId,
            @Body Map<String, Object> request
    );
    
    // 服务器状态检查接口
    @GET("api/status")
    Call<ResponseBody> checkServerStatus();

    /**
     * 获取广告列表
     */
    @GET("api/advertisement/list")
    Call<AdvertisementListResponse> getAdvertisements(@Header("Authorization") String token);

    /**
     * 获取公告列表
     */
    @GET("announcement/list")
    Call<AnnouncementListResponse> getAnnouncements(@Header("Authorization") String token);

    /**
     * 获取公告详情
     */
    @GET("announcement/{id}")
    Call<CommonResponse> getAnnouncementDetail(@Header("Authorization") String token, @Path("id") int id);

    /**
     * 获取任务列表
     */
    @GET("task/list")
    Call<TaskListResponse> getTasks(@Header("Authorization") String token);

    /**
     * 获取任务详情
     */
    @GET("task/{id}")
    Call<CommonResponse> getTaskDetail(@Header("Authorization") String token, @Path("id") int id);

    /**
     * 提交任务（不带图片）
     */
    @POST("task/submit")
    Call<CommonResponse> submitTask(@Header("Authorization") String token, @Body Map<String, String> taskData);

    /**
     * 提交任务（带图片）
     */
    @Multipart
    @POST("task/submit")
    Call<CommonResponse> submitTaskWithImage(
            @Header("Authorization") String token,
            @Part("title") RequestBody title,
            @Part("link") RequestBody link, 
            @Part("content") RequestBody content,
            @Part MultipartBody.Part image
    );

    /**
     * 获取项目列表
     */
    @GET("project/list")
    Call<ProjectListResponse> getProjects(@Header("Authorization") String token);

    /**
     * 获取项目详情
     */
    @GET("project/{id}")
    Call<CommonResponse> getProjectDetail(@Header("Authorization") String token, @Path("id") int id);

    /**
     * 获取悬赏列表
     */
    @GET("reward/list")
    Call<RewardListResponse> getRewards(@Header("Authorization") String token);

    /**
     * 获取悬赏详情
     */
    @GET("reward/{id}")
    Call<CommonResponse> getRewardDetail(@Header("Authorization") String token, @Path("id") int id);

    /**
     * 申请悬赏
     */
    @POST("reward/apply")
    Call<CommonResponse> applyReward(@Header("Authorization") String token, @Body Map<String, Object> rewardData);

    /**
     * 获取邀请码
     */
    @GET("user/invite/code")
    Call<InviteCodeResponse> getInviteCode(@Query("userId") String userId);

    /**
     * 获取邀请记录
     */
    @GET("user/invitations")
    Call<InvitationResponse> getInvitations(@Query("userId") String userId);

    /**
     * 使用邀请码注册
     */
    @FormUrlEncoded
    @POST("user/register-with-invite")
    Call<CommonResponse> registerWithInvite(
            @Field("username") String username,
            @Field("password") String password,
            @Field("userNickname") String userNickname,
            @Field("inviteCode") String inviteCode
    );

    /**
     * 更新用户信息
     */
    @POST("admin/user/update")
    Call<CommonResponse> updateUser(@Header("Authorization") String token, @Body Map<String, Object> userData);

    /**
     * 更新用户密码
     */
    @POST("admin/user/password")
    Call<CommonResponse> updatePassword(@Header("Authorization") String token, @Body Map<String, Object> passwordData);

    /**
     * 管理员发布悬赏
     */
    @POST("admin/reward/publish")
    Call<CommonResponse> publishReward(@Header("Authorization") String token, @Body Map<String, Object> rewardData);

    /**
     * 管理员发布悬赏（带图片）
     */
    @Multipart
    @POST("admin/reward/publish")
    Call<CommonResponse> publishRewardWithImage(
            @Header("Authorization") String token,
            @Part("title") RequestBody title,
            @Part("description") RequestBody description,
            @Part("amount") RequestBody amount,
            @Part MultipartBody.Part image
    );

    /**
     * 管理员发布任务
     */
    @POST("admin/task/publish")
    Call<CommonResponse> publishTask(@Header("Authorization") String token, @Body Map<String, Object> taskData);

    /**
     * 管理员发布广告
     */
    @POST("admin/advertisement/publish")
    Call<AdvertisementResponse> publishAdvertisement(
        @Header("Authorization") String token,
        @Body Map<String, String> advertisementData
    );

    /**
     * 管理员发布公告
     */
    @POST("admin/announcement/publish")
    Call<CommonResponse> publishAnnouncement(@Header("Authorization") String token, @Body Map<String, Object> announcementData);

    @GET("reward/task/{taskId}")
    Call<RewardTask> getRewardTask(@Path("taskId") int taskId);

    @GET("reward/tasks")
    Call<List<RewardTask>> getRewardTasks();

    @FormUrlEncoded
    @POST("reward/task/take")
    Call<CommonResponse> takeRewardTask(
        @Field("username") String username,
        @Field("taskId") int taskId
    );

    @PUT("admin/user/{userId}/update")
    Call<CommonResponse> updateUserAndPassword(
        @Path("userId") int userId,
        @Body Map<String, Object> userData
    );

    @POST("admin/user/create")
    Call<CommonResponse> createUser(@Header("Authorization") String token, @Body Map<String, Object> userData);

    @DELETE("admin/user/{userId}")
    Call<CommonResponse> deleteUser(@Header("Authorization") String token, @Path("userId") int userId);

    @POST("admin/user/simulate-task")
    Call<CommonResponse> simulateTask(@Header("Authorization") String token, @Body Map<String, Object> taskData);

    @GET("admin/tasks/pending")
    Call<CommonResponse> getPendingTasks(@Header("Authorization") String token);

    @POST("admin/tasks/approve")
    Call<CommonResponse> approveTask(@Header("Authorization") String token, @Body Map<String, Object> approveData);

    @POST("admin/tasks/reject")
    Call<CommonResponse> rejectTask(@Header("Authorization") String token, @Body Map<String, Object> rejectData);

    // 获取所有悬赏项目
    @GET("projects")
    Call<List<Project>> getAllProjects(@Header("Authorization") String token);

    // 获取单个悬赏项目详情
    @GET("projects/{id}")
    Call<Project> getProjectById(@Header("Authorization") String token, @Path("id") int projectId);

    // 创建新悬赏项目
    @POST("projects")
    Call<Project> createProject(@Header("Authorization") String token, @Body Project project);

    // 更新悬赏项目
    @PUT("projects/{id}")
    Call<Project> updateProject(@Header("Authorization") String token, @Path("id") int projectId, @Body Project project);

    // 删除悬赏项目
    @DELETE("projects/{id}")
    Call<Void> deleteProject(@Header("Authorization") String token, @Path("id") int projectId);

    // 申请悬赏项目
    @POST("projects/{id}/apply")
    Call<Void> applyForProject(@Header("Authorization") String token, @Path("id") int projectId);

    // 上传图片
    @Multipart
    @POST("upload")
    Call<String> uploadImage(@Header("Authorization") String token, @Part MultipartBody.Part image);

    // 上传广告图片
    @Multipart
    @POST("advertisements/upload")
    Call<CommonResponse> uploadAdImage(@Header("Authorization") String token, @Part MultipartBody.Part image);

    // 创建广告
    @POST("advertisements")
    Call<CommonResponse> createAdvertisement(@Header("Authorization") String token, @Body Map<String, Object> adData);

    // 删除广告
    @DELETE("advertisements/{id}")
    Call<CommonResponse> deleteAdvertisement(@Header("Authorization") String token, @Path("id") int adId);

    // 更新广告
    @PUT("advertisements/{id}")
    Call<CommonResponse> updateAdvertisement(@Header("Authorization") String token, @Path("id") int adId, @Body Map<String, Object> adData);

    // 上传项目图片
    @Multipart
    @POST("projects/upload")
    Call<CommonResponse> uploadProjectImage(@Header("Authorization") String token, @Part MultipartBody.Part image);

    @GET("users")
    Call<UserListResponse> getUserList(
            @Header("Authorization") String token,
            @Query("status") String status,
            @Query("level") String level,
            @Query("keyword") String keyword
    );
    
    @POST("users")
    Call<CommonResponse> addUser(
            @Header("Authorization") String token,
            @Body Map<String, Object> userData
    );
    
    @PUT("users/{userId}")
    Call<CommonResponse> updateUser(
            @Header("Authorization") String token,
            @Path("userId") int userId,
            @Body Map<String, Object> userData
    );
    
    @DELETE("users/{userId}")
    Call<CommonResponse> deleteUserById(
            @Header("Authorization") String token,
            @Path("userId") int userId
    );
    
    @GET("users/{userId}/tasks")
    Call<CommonResponse> getUserRewardTasks(
            @Header("Authorization") String token,
            @Path("userId") int userId
    );

    // 获取用户推荐人的API方法
    @GET("user/inviter")
    Call<InviterResponse> getUserInviter(@Header("Authorization") String token, @Query("username") String username);
    
    // 更新任务状态
    @PUT("tasks/{taskId}")
    Call<CommonResponse> updateTask(@Header("Authorization") String token, @Path("taskId") int taskId, @Body Map<String, Object> updateData);
    
    // 更新悬赏任务
    @PUT("reward/tasks/{taskId}")
    Call<CommonResponse> updateRewardTask(@Header("Authorization") String token, @Path("taskId") int taskId, @Body Map<String, Object> updateData);

    // 更新用户头像
    @PUT("users/avatar")
    Call<CommonResponse> updateUserAvatar(@Header("Authorization") String token, @Body Map<String, Object> userData);
}

